import { BasicForm } from '@/components/ProForm/interface/index';

import { defaultStyle } from '@/basic/style';
import { Dictionary } from '@/api/interface/basic/dictionary';
import { PublicInterface } from '@/interface/public';

export function useAddForm(dataGridRef: PublicInterface.ProTableValue) {
  let addForm = reactive<BasicForm>({
    form: {
      dicCode: '',
      dcode: '',
      showOrder: '',
      pid: 0,
      remark: ''
    },
    title: '',
    minWidth: 660,
    labelWidth: [100, 120],
    visible: false,
    formAttrs: {},
    formCols: 1,
    options: [
      {
        label: 'id',
        prop: 'id',
        hide: true,
        type: 'input'
      },
      {
        label: 'catalogId',
        prop: 'catalogId',
        hide: true,
        type: 'input'
      },
      {
        type: 'input',
        label: '字典标识',
        hide: false,
        prop: 'dicCode',
        defaultValue: '',
        attrs: {
          disabled: true
        },
        rules: [{ required: true, message: '字典名称', trigger: 'blur' }]
      },
      {
        type: 'input',
        label: 'dictionaryValue',
        hide: false,
        prop: 'dicValue',
        defaultValue: '',
        rules: [{ required: true, message: 'dictionaryValue', trigger: 'blur' }],
        attrs: {
          placeholder: '字典值,多个以;分隔'
        }
      },
      {
        type: 'input',
        label: '参数值',
        hide: false,
        prop: 'paramMark',
        defaultValue: ''
      },

      {
        type: 'input',
        label: '显示顺序',
        defaultValue: '',
        hide: false,
        prop: 'showOrder',
        rules: [{ validator: validateNumber1, trigger: 'blur' }]
      },
      {
        type: 'input',
        label: 'remark',
        defaultValue: '',
        hide: false,
        prop: 'remark',
        attrs: {
          style: defaultStyle
        }
      }
    ]
  });

  const getPidOptions = async (type: string) => {
    // let data = await dictionaryCodesByType({ type });
    // const newData = data.map(item => {
    //   const { dicMark, dicCode } = item;
    //   console.log(dicMark, 'dicMark');
    //   return {
    //     label: dicMark,
    //     value: dicCode,
    //     leaf: true,
    //     type: 'option'
    //   };
    // });
    // return newData;
  };

  const saveAddForm = async (form: Dictionary.ReqDoSave) => {
    const _params = JSON.parse(JSON.stringify(form));

    _params.id ? await dictUpdate(_params) : await dictSave(_params);
    addForm.visible = false;
    dataGridRef.value.getTableList();
    elMessage();
  };
  return { addForm, saveAddForm, getPidOptions };
}
